//
//  GXMenuChangeController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/13.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXMenuChangeController.h"
#import "ImagePickerTool.h"
#import "OrderAndUploadPhotoMananger.h"
#import "GXCommonIntroduceController.h"
#import "GXGroupSelectController.h"
#import "GXMenuGroupModel.h"
@interface GXMenuChangeController ()
@property (weak, nonatomic) IBOutlet UIImageView *photoImageView;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *priceTF;
@property (weak, nonatomic) IBOutlet UITextField *discountPriceTF;
@property (weak, nonatomic) IBOutlet UILabel *menuLabel;
@property (weak, nonatomic) IBOutlet UILabel *groupLabel;
@property (strong, nonatomic)GXMenuGroupModel *groupModel;
@property (strong, nonatomic) NSDictionary * contentDic;
@property (copy, nonatomic) NSString *imageStr;//图片路径
@property (copy, nonatomic) NSString *describleStr;//描述
@property (copy, nonatomic) NSString *groupId;//分组ID
@property (assign,nonatomic)BOOL imageTye;
@property (assign,nonatomic)BOOL describleTye;
@property (assign,nonatomic)BOOL groupTye;
@end

@implementation GXMenuChangeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"菜品编辑";
    self.view.backgroundColor = kGlobalbgColor;
    [self requestSingleMenuDetailData];
}
- (void)requestSingleMenuDetailData
{
    [MenuContentManager selectSingleDishWithServiceId:self.serviceId success:^(id responseObject) {
        [self handleMessageData:responseObject];
    } fail:^(id responseObject) {
        
    }];
}
- (void)handleMessageData:(id)responseObj
{
    NSDictionary * contentDic = responseObj[@"result"];
    self.contentDic = [NSDictionary dictionaryWithDictionary:contentDic];
    self.imageStr = [contentDic objectForKey:@"dishesImage"];
    NSString *UrlStr = [NSString stringWithFormat:@"%@/%@",kBaseURLIMAGE,self.imageStr];
    [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:UrlStr] placeholderImage:[UIImage imageNamed:@"placeHolder_Menu"]];
    self.nameTF.placeholder = [contentDic objectForKey:@"name"];
    self.priceTF.placeholder = [contentDic objectForKey:@"price"];
    self.discountPriceTF.placeholder = [contentDic objectForKey:@"discountPrice"];
    self.groupLabel.text = [contentDic objectForKey:@"dishesName"];
    if ([NSString isBlankString:contentDic[@"describer"]]) {
        self.menuLabel.text = @"未填写";
    }else{
        self.menuLabel.text = @"已填写";
    }
}

- (IBAction)uploadPhotoBtn:(UIButton *)sender {
    [[ImagePickerTool sharePicker] showImagePicker:self allowEdit:YES complete:^(id param) {
        [OrderAndUploadPhotoMananger uploadPhotoToServerFromImage:param fileName:MenuPic success:^(id responseObject) {
            if (responseObject) {
                self.photoImageView.image = param;
                self.imageTye = YES;
                self.imageStr = responseObject[@"describa"];
            }
        } fail:^(id responseObject) {
            
        }];
    }];
}

- (IBAction)pressMenuDescribleBtn:(id)sender {
    GXCommonIntroduceController * introduceVC =[[GXCommonIntroduceController alloc]init];
    introduceVC.titleName = @"菜品描述";
    introduceVC.placeHolderStr = @"请输入菜品描述(选填)";
    WeakSelf(weakSelf);
    introduceVC.describleBlock = ^(NSString * _Nonnull describleStr) {
        weakSelf.menuLabel.text = @"已填写";
        weakSelf.describleStr = describleStr;
        weakSelf.describleTye = YES;
    };
    [self.navigationController pushViewController:introduceVC animated:YES];
    
}
- (IBAction)pressGroupBtn:(id)sender {
    WeakSelf(weakSelf);
    GXGroupSelectController * selectVC = [[GXGroupSelectController alloc]init];
    selectVC.selectBlock = ^(GXMenuGroupModel * _Nonnull model) {
        weakSelf.groupModel = model;
        weakSelf.groupLabel.text = model.name;
        weakSelf.groupId = model.dishesId;
        weakSelf.groupTye = YES;
    };
    [self.navigationController pushViewController:selectVC animated:YES];
}
- (IBAction)pressSaveBtn:(id)sender {
    if (![self judgeValue]) {
        return;
    }
    NSString *dishesName = [NSString isBlankString:self.nameTF.text] ? self.nameTF.placeholder : self.nameTF.text;
    NSString *discountPrice = [NSString isBlankString:self.discountPriceTF.text] ? [NSString handleStrIsEmptyFrom:self.discountPriceTF.placeholder] : self.discountPriceTF.text;
    NSString *priceStr = [NSString isBlankString:self.priceTF.text] ? [NSString handleStrIsEmptyFrom:self.priceTF.placeholder] : self.priceTF.text;
    NSString *describerStr = [self judgeDescribleStrNullFromString:self.describleStr];
    NSString *serviceId = [NSString stringWithFormat:@"%@",self.serviceId];
    NSString *dishesId = [NSString stringWithFormat:@"%@",self.contentDic[@"dishesId"]];
    [MenuContentManager changeDishWithServiceId:serviceId dishesName:dishesName dishesImage:self.imageStr describerStr:describerStr dishesId:dishesId priceStr:priceStr discountPrice:discountPrice success:^(id responseObject) {
        if (responseObject) {
            if (self.saveSuccessBlock) {
                self.saveSuccessBlock();
            }
        }
    } fail:^(id responseObject) {

    }];
    self.imageTye = NO;
    self.groupTye = NO;
    self.describleTye = NO;
}
- (NSString *)judgeDescribleStrNullFromString:(NSString *)str
{
    if ([str isEqualToString:@"未填写"]) {
        return  @"";
    }else
    {
        if ([NSString isBlankString:self.describleStr]) {
            return self.menuLabel.text;
        }else
        {
            return self.describleStr;
        }
    }
}
- (BOOL)judgeValue
{
    if ((!self.groupTye)&&(!self.imageTye)&&(!self.describleTye)&&([NSString isBlankString:self.nameTF.text])&&([NSString isBlankString:self.priceTF.text])&&([NSString isBlankString:self.discountPriceTF.text]) ) {
        [SVProgressHUD showErrorWithStatus:@"未做修改"];
        return NO;
    }
    return YES;
}

@end
